package com.yaglyf.mvc.config.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.yaglyf.mvc.controller.vo.Result.Result;
import com.yaglyf.mvc.dao.entity.UserInfo;
import com.yaglyf.mvc.emun.Constant;
import com.yaglyf.mvc.emun.ResultEmun;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * Title: Class CommentsShow
 * Description:
 * 注释展示类，说明各种方法注释怎么写
 *
 * @author qiang.yang
 * @version 1.0.0
 * @email qiang.yang@chinaredstar.com
 * @date: 2016/10/9 10:16
 */
@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {

    //前置 handle 可以进行编码、安全控制等处理；
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        UserInfo o = (UserInfo)session.getAttribute(Constant.J_SESSION.name());
        if (null != o){
            return true;
        }
//        //如果是ajax请求响应头会有x-requested-with
        if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            try (PrintWriter out = response.getWriter()) {
                Result result = new Result(ResultEmun.FAIL);
                out.print(JSONObject.toJSONString(result));
                out.flush();
            } catch (IOException e) {
            }
        } else {
            if (request.getRequestURI().contains("register")||request.getRequestURI().contains("login"))
                 return true;
            response.sendRedirect("/");
        }
        return false;
    }

    //页面渲染前 ，返回值 返回后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    //页面渲染完成，返回前台前  用于处理ex
    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    public static void main(String[] args) {
        Random random = new Random();
        int count = 0 ;
        int all = 1000000;

        for (int i = 0 ;i < all; i++){
            double x = random.nextDouble()*10;
            double y = random.nextDouble()*10;

            if (x*x + y*y < 100){
                count++;
            }

        }
        System.out.println(count*4);



    }
}
